import React from 'react';
import util from 'util';
import {Table,message, Modal,Button} from 'antd';
import {BatchSearch, RightButton} from 'kpl';
import sys_dict from "sys_dict";
import {BatchDetail} from './components/form';

// 服务管理
class TaskBatch extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tblSource: [],
            totalCount: 0,
            isLoading: false,
            key:0,
            projectList:[],
            defaultProKey:"",
            batchList:[],
            defaultBatchKey:"",
            executeList:[],
            executeResultList:[],
            colIndex:0,
            modalVisible:false
        };
        let cuDate=util.dateFormat(new Date(), "yyyyMMdd");
        // 初始化查询参数
        this.searchParams={
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString(),
            projectid:"",
            batchcode:"",
            dealdate:cuDate
        };
        // 批处理批次号
        this.batchsid="";
    }
    // 渲染之后
    componentDidMount() {
        // 获取项目编号
        util.httpRequest({
            type: "POST", url: 'task050201.json', params: {},
            success: function (result) {
                if (result.ret_code == "0000") {
                    let projectList=[];
                    for(let obj of result.rows){
                        projectList.push({itemkey:obj.id,itemval:obj.name});
                    }
                    let firstkey=projectList.length>0?projectList[0].itemkey:"";
                    console.log("list;",projectList);
                    this.searchParams.projectid=firstkey;
                    this.setState({
                        projectList:projectList,
                        defaultProKey: firstkey,
                       // key:Math.random()
                    });
                    // 查询批处理列表
                    this.projectBatch(firstkey);
                    // 刷新情况
                    // this.refresh(this.searchParams);
                }
            }.bind(this)
        });
    }

    // 请求项目 123qwe
    projectBatch(projectid){
        // 获取项目编号
        util.httpRequest({
            type: "POST", url: 'task050604.json', params: {projectid:projectid},
            success: function (result) {
                if (result.ret_code == "0000") {
                    let batchList=[];
                    for(let obj of result.rows){
                        batchList.push({itemkey:obj.batchcode,itemval:obj.batchname});
                    }
                    let firstkey=batchList.length>0?batchList[0].itemkey:"";
                    console.log("firstkey1;",firstkey);
                    this.searchParams.batchcode=firstkey;
                    this.setState({
                        batchList:batchList,
                        defaultBatchKey: firstkey,
                        key:Math.random()
                    });

                    // 刷新情况
                    this.refresh(this.searchParams);
                }
            }.bind(this)
        });
    }

    // 刷新 1
    refresh(paramdic) {
        // 打开等待框
        this.setState({isLoading: true});
        util.httpRequest({
            type: "POST", url: 'task050601.json', params: paramdic,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        colIndex:0,
                        tblSource: result.rows,
                        totalCount: result.total
                    });
                }
            }.bind(this)
            ,complete: function () {
                this.setState({
                    isLoading: false
                })
            }.bind(this)
        });
    }

    // 查询 qwe
    search(params) {
        // 分页初始化
        params.page = "1";
        params.rows = sys_dict.defaultPageSize.toString();
        params.dealdate = util.date2str(params.dealdate);
        this.searchParams=params;
        this.refresh(params);
    }

    // 详情
    detail(record, index){
        this.setState({
            // executeResultList: result.rows,
            modalVisible:true
        });
        this.searchParams.batchsid=record.batchsid;
    }

    // pagesize改变
    onShowSizeChange(current, pageSize) {
        console.log(current, pageSize);
        let params = this.searchParams;
        params.rows = pageSize;
        params.page = current;
        this.refresh(params);
    }
    // page改变
    onChange(page) {
        console.log(page);
        let params = this.searchParams;
        params.page = page;
        this.refresh(params);
    }

    // 初始化配置 wadfqw
    config() {

        let columnsList=[];
        // 项目任务列表
        columnsList[0]=[ {
            title: '项目名称',
            dataIndex: 'projectname',
            key: 'projectname',
            width:"100"
        },{
            title: '任务名称',
            dataIndex: 'jobname',
            key: 'jobname',
            width:"110"
        }, {
            title: '执行顺序',
            dataIndex: 'exeorder',
            key: 'exeorder',
            width:"110"
        }, {
            title: '执行成功笔数',
            dataIndex: 'succcnt',
            key: 'succcnt',
            width:"110"
        }, {
            title: '执行失败笔数',
            dataIndex: 'failcnt',
            key: 'failcnt',
            width:"110"
        },{
            title: '处理中笔数',
            dataIndex: 'ingcnt',
            key: 'ingcnt',
            width:"110"
        }];
        // 批处理执行日志 1
        columnsList[1]=[ {
            title: '批处理批次号',
            dataIndex: 'batchsid',
            key: 'batchsid',
            width:"200"
        },{
            title: '项目名称',
            dataIndex: 'projectname',
            key: 'projectname',
            width:"100"
        },{
            title: '批处理名称',
            dataIndex: 'batchname',
            key: 'batchname',
            width:"100"
        }, {
            title: '总任务数',
            dataIndex: 'total_jobnums',
            key: 'total_jobnums',
            width:"80"
        }, {
            title: '执行成功笔数',
            dataIndex: 'succcnt',
            key: 'succcnt',
            width:"100"
        }, {
            title: '执行失败笔数',
            dataIndex: 'failcnt',
            key: 'failcnt',
            width:"100"
        },{
            title: '处理中笔数',
            dataIndex: 'ingcnt',
            key: 'ingcnt',
            width:"80"
        },{
            title: '处理日期',
            dataIndex: 'dealdate',
            key: 'dealdate',
            width:"110"
        },{
            title: '处理结果描述',
            dataIndex: 'statusmsg',
            key: 'statusmsg',
            width:"110"
        }, {
            title: '用户操作',
            key: 'operation',
            width:"150",
            render: (text, record, index) => (
                <span>
                    <a onClick={this.detail.bind(this,record,index)}>详情</a>
                </span>
            )
        },];

        // 返回列表配置
        return {
            tableConfig: {
                loading: this.state.isLoading,
                dataSource: this.state.tblSource,
                columns: columnsList[this.state.colIndex]
            }
        };
    }

    // 执行 24
    execute(){
        // 打开等待框
        this.setState({isLoading: true});
        util.httpRequest({
            type: "POST", url: 'task050602.json', params: this.searchParams,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.batchsid=result.result;
                    message.success('执行成功');
                }
            }.bind(this)
            ,complete: function () {
                this.setState({
                    isLoading: false
                })
            }.bind(this)
        });
    }

    // 查询执行情况列表 12321312eqweq
    searchExecuteList(){
        util.httpRequest({
            type: "POST", url: 'task050605.json', params: this.searchParams,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        tblSource: result.rows,
                        colIndex:1
                    });
                }
            }.bind(this)
        });
    }

    // 查询执行结果
    searchExecute(){
        this.searchParams.batchsid=this.batchsid;
        util.httpRequest({
            type: "POST", url: 'task050603.json', params: this.searchParams,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        executeResultList: result.rows,
                    });
                }
            }.bind(this)
        });
    }

    handleCancel(){
        this.setState({
            modalVisible:false
        });
    }

    // 渲染  2234we
    render() {
        let config = this.config();

        let currentDate=new Date();
        const searchConfig = {
            searchTitle: "查询日志",
            searchInit: {
                projectid:this.state.defaultProKey,
                dealdate:currentDate,
                batchcode:this.state.defaultBatchKey
            },
            searchField: [
                [
                    {propsname: "项目编号", propsKey: "projectid", propsType: "select", key: "1111",optionData:this.state.projectList},
                    {propsname: "批处理编号",propsKey: "batchcode",propsType: "select",key: "1113",optionData:this.state.batchList},
                    {propsname: "处理日期", propsKey: "dealdate", propsType: "datepicker", key: "6666"}
                ]
            ]
        };

        return (
            <div>
                <BatchSearch key={this.state.key} execute={this.execute.bind(this)} searchExecute={this.searchExecuteList.bind(this)} search={this.search.bind(this)} searchConfig={searchConfig}></BatchSearch>
                <Table {...config.tableConfig}></Table>
                <Modal visible={this.state.modalVisible} width="70%"
                       closable={false} footer={[
                        <Button key="back" type="ghost" size="large" onClick={this.handleCancel.bind(this)}>取消</Button>,
                    ]}>
                    <BatchDetail key={Math.random()} searchParams={this.searchParams} tblSource={this.state.executeResultList} ref="form" ></BatchDetail>
                </Modal>
            </div>
        );
    }
}

export default TaskBatch;